package com.zq.zhufengfm;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.zq.zhufengfm.fragments.CustomerTingFragment;
import com.zq.zhufengfm.fragments.DiscoverFragment;
import com.zq.zhufengfm.fragments.DownLoadTingFragment;
import com.zq.zhufengfm.fragments.PersonalFragment;


public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {
//    主界面第一层Fragment发现，定制听，下载听，我
    private Fragment fragments[];

//    private FragmentTransaction tx;
    private FragmentManager manager;
//    int current =0 ;//记录当前 fragment页
//    private static boolean isLoad = false;

    private RadioButton test;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //TODO检查横竖屏切换的时候   这个数组对象和内部元素是否置空：
        fragments = new Fragment[4];

        FragmentManager manager =
                getSupportFragmentManager();

        if(savedInstanceState != null){
            // 代表Activity重新创建，内部的Fragment会自动添加
            int len = fragments.length;

            for (int i = 0; i < len; i++) {
                fragments[i] = manager.findFragmentByTag("f" + i);
            }
        }else{
            fragments[0]=new DiscoverFragment();
            fragments[1]=new CustomerTingFragment();
            fragments[2]=new DownLoadTingFragment();
            fragments[3]=new PersonalFragment();
        }

        //采用  hide和show的方式处理：
        FragmentTransaction tx =
                manager.beginTransaction();

        int length = fragments.length;
        for (int i = 0; i < length; i++) {
            // 添加Fragment，并且指定Tag，利用 Tag，在Activity重新创建的时候
            // 进行恢复，查找
            tx.add(R.id.main_fragment_container, fragments[i], "f" + i);
            tx.hide(fragments[i]);
        }

        tx.show(fragments[0]);

        tx.commit();



        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.main_tan_bar);
        radioGroup.setOnCheckedChangeListener(this);
//
//        test = (RadioButton) findViewById(R.id.main_tab_discover);
//
//        if(savedInstanceState==null){
//
//            fragments = new Fragment[4];
//
//            fragments[0]=new DiscoverFragment();
//            fragments[1]=new CustomerTingFragment();
//            fragments[2]=new DownLoadTingFragment();
//            fragments[3]=new PersonalFragment();
//
//        }else{
//            fragments = (Fragment[]) savedInstanceState.getSerializable("fragments");
//            current = savedInstanceState
//                    .getInt("cur");
//            isLoad = true;
//        }
    }

//    @Override
//    protected void onResume() {
//        super.onResume();
//        //采用hide和show的形式，进行处理
//        Log.d("debug111", "执行onResume方法");
//        manager = getSupportFragmentManager();
//
//        tx = manager.beginTransaction();
//
//        for (int i = 0; i < fragments.length; i++) {
//            Log.d("debug111", "onResume 添加碎片");
//            tx.add(R.id.main_fragment_container, fragments[i], "tag" + i);
//            tx.hide(fragments[i]);
//        }
//        if(isLoad){
//            tx.show(fragments[current]);
//        }else
//            tx.show(fragments[0]);
//        tx.commit();
//    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int index=0;
        switch (checkedId){
            case R.id.main_tab_discover:
                index=0;
                break;
            case R.id.main_tab_custom:
                index=1;
                break;
            case R.id.main_tab_download:
                index=2;
                break;
            case R.id.main_tab_personal:
                index=3;
                break;
        }

        int len = fragments.length;
        manager=getSupportFragmentManager();
        FragmentTransaction tx = manager.beginTransaction();
        for (int i = 0; i < len; i++) {
            if (i==index){
                tx.show(fragments[i]);
            }else {
                tx.hide(fragments[i]);
            }
        }
        tx.commit();
    }

//    @Override
//    protected void onSaveInstanceState(Bundle outState) {
//
//        outState.putParselable("fragments", fragments);//不能   参数必须实现序列化
//        outState.putInt("cur",current);
//        tx = manager.beginTransaction();
//
//        for(int i=0;i<fragments.length;i++){
//
//            tx.remove(fragments[i]);
//
//        }
//        tx.commit();
//        super.onSaveInstanceState(outState);
//    }


}
